﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Liang.UI
{
    public partial class FormSelectFile : Form
    {
        /// <summary>
        /// 选中的文件
        /// </summary>
        public string SelectedFile
        {
            get;
            set;
        }

        /// <summary>
        /// 根文件夹
        /// </summary>
        string m_strRootPath = string.Empty;

        /// <summary>
        /// 选中的文件夹路径
        /// </summary>
        string m_strFolderPath = string.Empty;

        /// <summary>
        /// 文件类型
        /// </summary>
        string m_strExtension = "*";

        Bitmap m_bitmapNoView = null;

        public FormSelectFile(string strRootPath, string strExtension)
        {
            InitializeComponent();
            dateTimePicker_start.Value = DateTime.Now;
            dateTimePicker_end.Value = DateTime.Now;
            m_strRootPath = strRootPath;
            m_strExtension = strExtension;
        }

        private void FormSelectFile_Load(object sender, EventArgs e)
        {
            ResourceManager rm = new ResourceManager(typeof(FormSelectFile));
            m_bitmapNoView = (Bitmap)rm.GetObject("file_noView");
            initFolder();
        }

        /// <summary>
        /// 加载文件夹
        /// </summary>
        void initFolder()
        {
            DirectoryInfo dir = new DirectoryInfo(m_strRootPath);

            if (dir.Exists)
            {
                listView_folder.Items.Clear();
                foreach (DirectoryInfo d in dir.GetDirectories())
                {
                    if (d.Attributes == FileAttributes.Directory)
                    {
                        // 只加载文件夹
                        // 系统文件夹，隐藏文件夹均不加载
                        listView_folder.Items.Add(d.FullName, d.Name, 0);
                    }

                    //MessageBox.Show(d.Attributes.ToString());
                }
            }
        }

        /// <summary>
        /// 加载文件
        /// </summary>
        void initFiles(bool bQuery = false)
        {
            DirectoryInfo dir = new DirectoryInfo(m_strFolderPath);

            string strTmpExt = m_strExtension;
            if ("*" != m_strExtension)
            {
                m_strExtension.Trim(new char[] { '.' });
                strTmpExt = string.Format(".{0}", m_strExtension);
            }

            if (dir.Exists)
            {
                listView_files.Items.Clear();
                int nIndex = 1;

                FileInfo[] childs = dir.GetFiles();
                foreach (FileInfo child in childs)
                {
                    if ("*" != strTmpExt)
                    {
                        if (0 == string.Compare(strTmpExt, child.Extension, true))
                        {
                            if (bQuery)
                            {
                                if (dateTimePicker_start.Value <= child.CreationTime && child.CreationTime <= dateTimePicker_end.Value)
                                {
                                    insertFileItem(nIndex, child);
                                    nIndex++;
                                }
                            }
                            else
                            {
                                insertFileItem(nIndex, child);
                                nIndex++;
                            }
                        }
                    }
                    else
                    {
                        if (bQuery)
                        {
                            if (dateTimePicker_start.Value <= child.CreationTime && child.CreationTime <= dateTimePicker_end.Value)
                            {
                                insertFileItem(nIndex, child);
                                nIndex++;
                            }
                        }
                        else
                        {
                            insertFileItem(nIndex, child);
                            nIndex++;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 预览图片
        /// </summary>
        /// <param name="strImageFile"></param>
        void preView(string strImageFile)
        {
            try
            {
                pictureBox_preView.BackgroundImage = Image.FromFile(strImageFile);
            }
            catch
            {
                pictureBox_preView.BackgroundImage = m_bitmapNoView;
            }
        }

        void insertFileItem(int nIndex, FileInfo fileInfo)
        {
            ListViewItem item = listView_files.Items.Add(fileInfo.FullName, nIndex.ToString(), 1);
            item.SubItems.Add(fileInfo.Name);
            item.SubItems.Add(fileInfo.CreationTime.ToString("yyyy-MM-dd HH:mm:ss"));
            item.SubItems.Add(fileInfo.FullName);
        }

        private void listView_folder_MouseClick(object sender, MouseEventArgs e)
        {
            if (0 < listView_folder.SelectedItems.Count)
            {
                // 选中了某一项
                m_strFolderPath = listView_folder.SelectedItems[0].Name;
                initFiles();
            }
        }

        private void listView_files_MouseClick(object sender, MouseEventArgs e)
        {
            if (0 < listView_files.SelectedItems.Count)
            {
                // 选中了某一项
                SelectedFile = listView_files.SelectedItems[0].Name;
                preView(SelectedFile);
            }
        }

        private void button_browse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderDialog = new FolderBrowserDialog();
            folderDialog.ShowNewFolderButton = false;
            folderDialog.SelectedPath = m_strRootPath;
            if (DialogResult.OK == folderDialog.ShowDialog())
            {
                m_strRootPath = folderDialog.SelectedPath;
                initFolder();
            }
        }

        private void button_query_Click(object sender, EventArgs e)
        {
            initFiles(true);
        }

        private void button_ok_Click(object sender, EventArgs e)
        {
            if (!File.Exists(SelectedFile))
            {
                MessageBox.Show("请选择一个文件", "注意", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
